using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NHibernate;
using tKitchen;


namespace tKitchen
{
    public partial class DALTestForm1 : Form
    {
        DataAccess da;
        public DALTestForm1()
        {
            InitializeComponent();

        }

        private void DALTestForm1_Load(object sender, EventArgs e)
        {
            da = new DataAccess();

            ISession session = da.Factory.OpenSession();

            //represents a NHibernate managed Transaction
            ITransaction transaction = session.BeginTransaction();

            UnitPO cup = new UnitPO();
            //myUnit.UnitId = 0;
            cup.Name = "Cup";
            cup.Abbrev = "C";
            cup.Descr = "Standard volume measurement";
            session.Save(cup);

            UnitPO tsp = new UnitPO();
            tsp.Abbrev = "Tsp";
            tsp.Descr = "Standard system unit of volume";
            tsp.Name = "Teaspoon";
            session.Save(tsp);

            IngredientCategoryPO other = new IngredientCategoryPO();
            //other.IngredientCategoryId = 123;
            other.Name = "Other";
            other.Descr = "Other ingredients that don't fit into another category";
            session.Save(other);

            RecipeCategoryPO deserts = new RecipeCategoryPO();
            deserts.Name = "Deserts";
            deserts.Descr = "Cakes, pies, coblers and other yummy things";
            session.Save(deserts);




            IngredientPO sugar = new IngredientPO();
            //myIng.IngredientId = 57;
            sugar.Category = other;
            sugar.Name = "Sugar";
            sugar.Descr = "Plain white sugar";
            session.Save(sugar);

            IngredientPO eggs = new IngredientPO();
            eggs.Category = other;
            eggs.Name = "Eggs";
            eggs.Descr = "Medium to large chicken eggs";
            session.Save(eggs);


            StepPO myStep = new StepPO();
            myStep.Descr = "mix all the ingredients together";
            session.Save(myStep);


            RecipeCommentPO myComment = new RecipeCommentPO();
            myComment.Comment = "this recipe is a really good one. Tried and true!";
            myComment.Date = DateTime.Today;
            session.Save(myComment);



            RecipePO myRecipe = new RecipePO();
            myRecipe.Name = "Pecan Pie";
            myRecipe.Rating = 9.75;
            myRecipe.Descr = "The best pie ever!!";
            myRecipe.Category = deserts;
            List<StepPO> stepList = new List<StepPO>();
            stepList.Add(myStep);
            myRecipe.Steps = stepList;
            List<Recipe_IngredientPO> ingredientList = new List<Recipe_IngredientPO>();
            Recipe_IngredientPO ing1 = new Recipe_IngredientPO();
            ing1.Amount = 2;
            ing1.Unit = cup;
            ing1.Ingredient = sugar;
            ingredientList.Add(ing1);

            Recipe_IngredientPO ing2 = new Recipe_IngredientPO();
            ing2.Amount = 2;
            ing2.Unit = null;
            ing2.Ingredient = eggs;
            ingredientList.Add(ing2);

            myRecipe.Ingredients = ingredientList;
            List<RecipeCommentPO> commentList = new List<RecipeCommentPO>();
            commentList.Add(myComment);
            myRecipe.Comments = commentList;

            session.Save(myRecipe);


            transaction.Commit();
            session.Flush();
            session.Close();







            session = da.Factory.OpenSession();
            List<IngredientCategoryPO> catList = (List<IngredientCategoryPO>)session.CreateCriteria(typeof(IngredientCategoryPO)).List<IngredientCategoryPO>();
            comboBox1.DataSource = catList;
            comboBox1.DisplayMember = "Name";
            comboBox1.ValueMember = "IngredientCategoryId";
            List<IngredientPO> ingList = (List<IngredientPO>)session.CreateCriteria(typeof(IngredientPO)).List<IngredientPO>();
            dataGridView1.DataSource = ingList;
            dataGridView1.Columns["IngredientId"].Visible = false;

            session.Close();


            /*

            //get all the users
            IList userList = session.CreateCriteria(typeof(User)).List();
            foreach (User user in userList)
            {
                Console.Out.WriteLine(user.UserName + " last logged in at " + user.LastLogon);
            }
            */
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ISession session = da.Factory.OpenSession();

            
            IngredientPO newIng = new IngredientPO();
            newIng.Name = textBox2.Text;
            newIng.Descr = textBox3.Text;

            
            IngredientCategoryPO newCat = (IngredientCategoryPO)session.Get(typeof(IngredientCategoryPO), comboBox1.SelectedValue);

            newIng.Category = newCat;

            session.Save(newIng);

            List<IngredientPO> ingList = (List<IngredientPO>)session.CreateCriteria(typeof(IngredientPO)).List<IngredientPO>();
            dataGridView1.DataSource = ingList;
            dataGridView1.Refresh();


        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            ISession session = da.Factory.OpenSession();

            Console.Out.WriteLine("dataGridView1_CellEndEdit event for row " + e.RowIndex);
            //dataGridView1.Rows[e.RowIndex]
            List<IngredientPO> blah = (List<IngredientPO>)dataGridView1.DataSource;
            IngredientPO tempIng = blah[e.RowIndex];
            session.Update(tempIng, tempIng.IngredientId);

            session.Flush();
            session.Close();

        }
    }
}